<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card shadow="never" v-loading="loading">
        <!-- 搜索栏 -->
        <el-form
          :inline="true"
          :model="pagination"
          ref="pagination"
        >
          <el-form-item label="标题：" prop="title">
            <el-input v-model="pagination.title" placeholder></el-input>
          </el-form-item>
          <el-form-item label="封面图片：" prop="cover">
            <el-input v-model="pagination.cover" placeholder></el-input>
          </el-form-item>
          <el-form-item label="分类id：" prop="blogCategoryId">
            <el-input v-model="pagination.blogCategoryId" placeholder></el-input>
          </el-form-item>
          <el-form-item label="排序：" prop="index">
            <el-input v-model="pagination.index" placeholder></el-input>
          </el-form-item>
          <el-form-item label="链接：" prop="link">
            <el-input v-model="pagination.link" placeholder></el-input>
          </el-form-item>
          <el-form-item label="内容主体：" prop="content">
            <el-input v-model="pagination.content" placeholder></el-input>
          </el-form-item>
          <el-form-item label="状态：1为未发布，2为已发布：" prop="status">
            <el-input v-model="pagination.status" placeholder></el-input>
          </el-form-item>
          <el-form-item label="发布人：" prop="creator">
            <el-input v-model="pagination.creator" placeholder></el-input>
          </el-form-item>
          <el-form-item label="创建时间：" prop="createTime">
            <el-input v-model="pagination.createTime" placeholder></el-input>
          </el-form-item>
          <el-form-item label="更新时间：" prop="updateTime">
            <el-input v-model="pagination.updateTime" placeholder></el-input>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="handleSearch">查询</el-button>
            <el-button @click="handleRest">重置</el-button>
          </el-form-item>
        </el-form>
        <!-- 搜索栏 / -->
        <div class="btnBox" style="text-align:right">
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </div>
        <!-- 数据表格 -->
        <el-table
          :data="items"
          border
          style="width: 100%; margin-top:10px;"
          @selection-change="handleSelectionChange"
        >
          <el-table-column prop="id" label="id"></el-table-column>
          <el-table-column prop="title" label="标题"></el-table-column>
          <el-table-column prop="cover" label="封面图片"></el-table-column>
          <el-table-column prop="blogCategoryId" label="分类id"></el-table-column>
          <el-table-column prop="index" label="排序"></el-table-column>
          <el-table-column prop="link" label="链接"></el-table-column>
          <el-table-column prop="content" label="内容主体"></el-table-column>
          <el-table-column prop="status" label="状态：1为未发布，2为已发布"></el-table-column>
          <el-table-column prop="creator" label="发布人"></el-table-column>
          <el-table-column prop="createTime" label="创建时间"></el-table-column>
          <el-table-column prop="updateTime" label="更新时间"></el-table-column>

          <el-table-column label="操作" width="100">
            <template slot-scope="scope">
              <el-button @click="handleEdit(scope.row)" type="text" size="small">修改</el-button>
              <el-button @click="handleDelect(scope.row)" type="text" size="small">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          class="pagination"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pagination.currentPage"
          :page-sizes="[10, 20, 30, 50]"
          :page-size="pagination.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="Number(total)"
        ></el-pagination>
        <!-- 数据表格 / -->
      </el-card>
      <!-- 新增标签弹层 -->
      <Dialog ref="addType" :dialogTitle="dialogTitle" v-on:newDataes = subMitForm></Dialog>
    </div>
  </div>
</template>

<script>
import { getList, getDetails, delData } from '@/api/blog'
import Dialog from './../components/blogAdd'
export default {
  name: 'blogList',
  components: {
    Dialog
  },
  data() {
    return {
      items: [
        {
        }
      ],
      total: 20,
      pagination: {
        page: 1,
        pageSize: 10,
        vehicleOwnerName: this.vehicleOwnerName,
        mobilePhone: this.mobilePhone
      },
      loading: false,
      multipleSelection: [],
      dialogVisible: false,
      dialogTitle: ''
    }
  },
  created() {
    this.doQuery()
  },
  methods: {
    // 业务方法
    async doQuery(parent) {
      this.loading = true
      // this.items = []
      await getList(parent)
        .then(res => {
          const { data } = res
          this.items = data.items
          this.total = data.counts
          this.loading = false
        })
        .catch(err => {
          console.log(err)
          this.loading = false
        })
    },
    // UI方法 - 重置
    handleRest() {
      this.$refs['pagination'].resetFields()
    },
    // 搜索
    handleSearch() {
      this.doQuery(this.pagination)
    },
    //
    handleSelectionChange(val) {
      console.log(val)
      this.multipleSelection = val
    },
    // 每页条数
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`)
      this.pagination.pageSize = val
      this.doQuery(this.pagination)
    },
    // 当前页
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`)
      this.pagination.page = val
      this.doQuery(this.pagination)
    },
    // 添加
    handleAdd(val) {
      this.dialogTitle = '添加'
      this.$refs.addType.dialogFormV()
    },
    // 详情获取  - 如果有详情接口 读详情
    async getDetails(id) {
      let dataes = {}
      await getDetails(id)
        .then(res => {
          const { data } = res
          this.loading = false
          dataes = data
        })
        .catch(err => {
          console.log(err)
          this.loading = false
        })
      return dataes
    },
    // 编辑
    async handleEdit (val) {
      this.dialogTitle = '编辑'
      // 如果有详情接口 读详情
      const data = await this.getDetails(val.id)
      // 如果列表中的数据可以满足修改的信息展示  则不用读取上面的详情接口 用下面的data
      // const data = val
      this.$refs.addType.dialogFormV(data)
    },
    // 删除
    handleDelect(item) {
      this.$confirm('是否确认删除？')
        .then(async ret => {
          await delData(item.id)
            .then(res => {
              console.log(res)
              this.loading = false
              this.$message({
                message: '删除成功',
                type: 'success'
              })
              this.doQuery(this.pagination)
            })
            .catch(err => {
              console.log(err)
              this.loading = false
            })
        })
        .catch(ret => {
          console.log(ret)
        })
    },
    // subMitForm
    subMitForm(val) {
      if (val.id) {
        this.$message.success('修改成功')
      } else {
        this.$message.success('添加成功')
      }
      this.doQuery(this.pagination)
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.alert {
  margin: 10px 0px 0px 0px;
}
.pagination {
  margin-top: 10px;
  text-align: right;
}
</style>
